/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.widget;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;

public class AnimationLayout extends FrameLayout {
	
	ArrayList<Animation> mAnimations = new ArrayList<Animation>();

	public AnimationLayout(Context context) {
		super(context);
	}
	
	public AnimationLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public AnimationLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	private void addAnimationToList(final View view, final Animation animation, final AnimationListener listener) {
		synchronized (animation) {
			Log.v("sw6", "Adding animation");
			mAnimations.add(animation);
		}
		
		AnimationListener al = new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				if (listener != null) listener.onAnimationStart(animation);
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
				if (listener != null) listener.onAnimationRepeat(animation);
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				if (listener != null) listener.onAnimationEnd(animation);
				view.setVisibility(INVISIBLE);
				synchronized (mAnimations) {
					Log.v("sw6", "Removing animation");
					mAnimations.remove(animation);
				}
			}
		};
		animation.setAnimationListener(al);
	}
	
	public void startAnimationInView(final View view, final Animation animation, final AnimationListener listener) {
		if (mAnimations.isEmpty()) {
			Log.i("sw6", "Animation list is empty, cleaing all children");
			removeViews(1, getChildCount() - 1);
		}
		
		addAnimationToList(view, animation, listener);
		
		addView(view);
		view.startAnimation(animation);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		synchronized (mAnimations) {
			if (!mAnimations.isEmpty()) {
				Log.v("sw6", "Preventing touch");
				return true;
			}
		}
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		synchronized (mAnimations) {
			if (!mAnimations.isEmpty()) {
				Log.v("sw6", "Preventing touch");
				return true;
			}
		}
		return super.onTouchEvent(event);
	}

}
